package com.xuzao.developer.service.impl;

import com.google.common.collect.Lists;
import com.xuzao.developer.base.model.ProjectVersion;
import com.xuzao.developer.dao.ProjectVersionDao;
import com.xuzao.developer.dto.ProjectVersionDTO;
import com.xuzao.developer.service.ProjectVersionService;
import com.xuzao.developer.utils.DateUtil;
import com.xuzao.developer.vo.BaseResultVo;
import com.xuzao.developer.vo.ProjectVersionVO;
import com.xuzao.developer.tools.entity.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @description 
 * @author xuzao
 * @date 2019-08-09 18:12:00
 **/
@Service("projectVersionService")
public class ProjectVersionServiceImpl implements ProjectVersionService {

	@Resource
	private ProjectVersionDao projectVersionDao;

	@Override
	public List<ProjectVersionVO> list(ProjectVersionDTO dto, Pager pager) {
		List<ProjectVersion> list = projectVersionDao.list(dto, pager).getList();
		List<ProjectVersionVO> listVo = Lists.newArrayList();
		if (!list.isEmpty()){
			list.forEach(o -> {
				ProjectVersionVO projectVersionVO = new ProjectVersionVO();
				BeanUtils.copyProperties(o, projectVersionVO);
				if (o.getPublishTime() != null) {
					projectVersionVO.setShowPublishTime(DateUtil.formatShort(o.getPublishTime()));
				}
				if (o.getPrePublishTime() != null) {
					projectVersionVO.setShowPrePublishTime(DateUtil.formatShort(o.getPrePublishTime()));
				}
				listVo.add(projectVersionVO);
			});
		}
		return listVo;
	}

	@Override
	public BaseResultVo insert(ProjectVersionDTO dto) {
		ProjectVersion projectVersion = new ProjectVersion();
		BeanUtils.copyProperties(dto,projectVersion);
		projectVersion.setPrePublishTime(DateUtil.parse(dto.getPrePublishTime()));
		projectVersion.setPublishTime(DateUtil.parse(dto.getPublishTime()));
		Integer count = projectVersionDao.insertSelective(projectVersion);
		if (count > 0) {
			return BaseResultVo.success();
		}
		return BaseResultVo.error();

	}

	@Override
	public BaseResultVo update(ProjectVersionDTO dto) {
		ProjectVersion projectVersion = new ProjectVersion();
		BeanUtils.copyProperties(dto,projectVersion);
		projectVersion.setPrePublishTime(DateUtil.parse(dto.getPrePublishTime()));
		projectVersion.setPublishTime(DateUtil.parse(dto.getPublishTime()));
		Integer count = projectVersionDao.updateSelectiveById(projectVersion);
		if (count > 0) {
			return BaseResultVo.success();
		}
		return BaseResultVo.error();
	}

	@Override
	public ProjectVersionVO get(ProjectVersionDTO dto) {
		ProjectVersion o = projectVersionDao.selectById(dto.getId());
		ProjectVersionVO projectVersionVO = new ProjectVersionVO();
		BeanUtils.copyProperties(o, projectVersionVO);
		if (o.getPublishTime() != null) {
			projectVersionVO.setShowPublishTime(DateUtil.formatShort(o.getPublishTime()));
		}
		if (o.getPrePublishTime() != null) {
			projectVersionVO.setShowPrePublishTime(DateUtil.formatShort(o.getPrePublishTime()));
		}
		return projectVersionVO;
	}

	@Override
	public void del(Long id) {
		projectVersionDao.deleteById(id);
	}
}